package com.laxcen.toydemo;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.laxcen.controls.HomeMenuButtonControl;
import com.laxcen.intelligenttoy.BLE.ETag;
import com.laxcen.lib.GameFailedDialog;
import com.laxcen.lib.GameSuccessDialog;

import util.BackgroundMusic;
import util.DelayHandler;
import util.RandomUtil;
import util.SoundClass;

public class GameStep3Activity extends BaseActivity implements View.OnClickListener {
    GameState mGameState = GameState.instance(this);
    HomeMenuButtonControl btn_apple1, btn_apple2, btn_apple_huanxiong, btn_home;
    HomeMenuButtonControl btn_option1, btn_option2, btn_option3;
    HomeMenuButtonControl btn_daxiao, btn_daku;
    HomeMenuButtonControl img_xiaoxiong_eat_apple;
    HomeMenuButtonControl btn_apple11, btn_apple12, btn_apple13, btn_apple14, btn_apple15, btn_apple16;
    LinearLayout layoutReminder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_step3);
        mGameState.start();

        btn_home = findViewById(R.id.btn_home);
        btn_home.setOnClickListener(this);

        btn_apple1 = findViewById(R.id.btn_apple1);
        btn_apple1.setOnClickListener(this);
        btn_apple2 = findViewById(R.id.btn_apple2);
        btn_apple2.setOnClickListener(this);
        btn_apple_huanxiong = findViewById(R.id.btn_apple_huanxiong);
        btn_apple_huanxiong.setOnClickListener(this);

        btn_option1 = findViewById(R.id.btn_option1);
        btn_option1.setOnClickListener(this);
        btn_option2 = findViewById(R.id.btn_option2);
        btn_option2.setOnClickListener(this);
        btn_option3 = findViewById(R.id.btn_option3);
        btn_option3.setOnClickListener(this);

        btn_daxiao = findViewById(R.id.btn_daxiao);
        btn_daxiao.setOnClickListener(this);
        btn_daku = findViewById(R.id.btn_daku);
        btn_daku.setOnClickListener(this);

        btn_apple11 = findViewById(R.id.btn_apple11);
        btn_apple12 = findViewById(R.id.btn_apple12);
        btn_apple13 = findViewById(R.id.btn_apple13);
        btn_apple14 = findViewById(R.id.btn_apple14);
        btn_apple15 = findViewById(R.id.btn_apple15);
        btn_apple16 = findViewById(R.id.btn_apple16);

        layoutReminder = findViewById(R.id.layoutReminder);

        hideXiaoxiong7Apple();

        img_xiaoxiong_eat_apple = findViewById(R.id.img_xiaoxiong_eat_apple);
    }

    private void startRepeatPromptGbSound() {
        layoutReminder.setVisibility(View.GONE);
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.GameStep3) {
                    BackgroundMusic.instance(GameStep3Activity.this).play(R.raw.audio_game_step3_dailai_6_apple, false);
                }
            }
        }, 2000).delay(new Runnable() {
            @Override
            public void run() {
                showXiaoxiong7Apple();
            }
        }, 5000).delay(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.GameStep3) {
                    BackgroundMusic.instance(GameStep3Activity.this).play(R.raw.audio_game_step3_how_many_total_apple, false);
                }
            }
        }, 4000).delay(new Runnable() {
            @Override
            public void run() {
                layoutReminder.setVisibility(View.VISIBLE);
            }
        }, 5000);
    }

    private void hideXiaoxiong7Apple() {
        btn_apple11.setVisibility(View.GONE);
        btn_apple12.setVisibility(View.GONE);
        btn_apple13.setVisibility(View.GONE);
        btn_apple14.setVisibility(View.GONE);
        btn_apple15.setVisibility(View.GONE);
        btn_apple16.setVisibility(View.GONE);

    }

    private void showXiaoxiong7Apple() {
        int delay = 300;
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                btn_apple11.show();
                btn_apple11.shake();
            }
        }, delay).delay(new Runnable() {
            @Override
            public void run() {
                btn_apple12.show();
                btn_apple13.shake();
            }
        }, delay).delay(new Runnable() {
            @Override
            public void run() {
                btn_apple13.show();
                btn_apple13.shake();
            }
        }, delay).delay(new Runnable() {
            @Override
            public void run() {
                btn_apple14.show();
                btn_apple14.shake();
            }
        }, delay).delay(new Runnable() {
            @Override
            public void run() {
                btn_apple15.show();
                btn_apple15.shake();
            }
        }, delay).delay(new Runnable() {
            @Override
            public void run() {
                btn_apple16.show();
                btn_apple16.shake();
            }
        }, delay);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_apple1 || v.getId() == R.id.btn_apple2) {
            HomeMenuButtonControl btn_apple = (HomeMenuButtonControl) v;
//            btn_apple.shake();
        } else if (v.getId() == R.id.btn_home) {
            goback();
        } else if (v.getId() == R.id.btn_option1) {
            onOptionErrorActive();
        } else if (v.getId() == R.id.btn_option2) {
            onOptionErrorActive();
        } else if (v.getId() == R.id.btn_option3) {
            onOption9Active();
        }
    }

    private void goback() {
        isWaitTag = false;
        BackgroundMusic.instance(this).stop();
        startActivityAndFinish(GameStepSelectActivity.class);
    }

    private void onOptionErrorActive() {
        btn_apple2.shake();
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {

                img_xiaoxiong_eat_apple.show();
                btn_daku.show();
                btn_daku.bigSplash();
                new SoundClass(GameStep3Activity.this).playSound(R.raw.audio_daku);
            }
        }, 1000).delay(new Runnable() {
            @Override
            public void run() {
                new SoundClass(GameStep3Activity.this).playSound(R.raw.audio_game_step3_error_prompt);
            }
        }, 3000).delay(new Runnable() {
            @Override
            public void run() {
                showFalied();
            }
        }, 5000);
    }

    private void onOption9Active() {
//        btn_apple1.fall();
        onRightOptionActive();
    }

    private void onRightOptionActive() {
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                img_xiaoxiong_eat_apple.show();
                btn_daxiao.show();
                btn_daxiao.bigSplash();
                new SoundClass(GameStep3Activity.this).playSound(R.raw.audio_daxiao);
            }
        }, 1000).delay(new Runnable() {
            @Override
            public void run() {
                new SoundClass(GameStep3Activity.this).playSound(R.raw.audio_game_step3_success_prompt);
//                btn_apple_huanxiong.show();
//                btn_apple_huanxiong.shake();
            }
        }, 3000).delay(new Runnable() {
            @Override
            public void run() {
                showSuccess();
            }
        }, 7000);
    }

    private void onOption2Active() {
        btn_apple1.fall();
        btn_apple2.fall();
        onRightOptionActive();
    }

    private void showSuccess() {
        GameSuccessDialog successDialog = GameSuccessDialog.getInstance(this);
        successDialog.show("", RandomUtil.generateOneNumber(0, 5));
        successDialog.setGobackClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goback();
            }
        });
        successDialog.setGoNextClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityAndFinish(GameStep4Activity.class);
            }
        });
    }

    private void showFalied() {
        GameFailedDialog failedDialog = GameFailedDialog.getInstance(this);
        failedDialog.show();
        failedDialog.setGobackClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goback();
            }
        });
        failedDialog.setGoNextClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                startActivity(GameStep3Activity.class);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        BackgroundMusic.instance(this).stop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGameState.setStep(GameState.EGameStep.GameStep3);
        startRepeatPromptGbSound();
        restartTagCheck();
    }

    private boolean isWaitTag = true;

    private void restartTagCheck() {
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                isWaitTag = true;
                startTagCheck();
            }
        }, 1000);
    }

    /* 7,8,9 = 1,2,3 */
    private void startTagCheck() {
        if (!isWaitTag) return;
        if (checkOption(currentTags, ETag._9)) {
            isWaitTag = false;
            onOption9Active();
        } else if (checkOption(currentTags, ETag._GOBACK)) {
            isWaitTag = false;
            goback();
        } else if (checkHasAnyTag(currentTags)) {
            isWaitTag = false;
            onOptionErrorActive();
        }

        if (isWaitTag) {
            DelayHandler.newHandler().delay(new Runnable() {
                @Override
                public void run() {
                    startTagCheck();
                }
            }, LoopDetectionMillis);
        }
    }
}
